import 'package:flutter/material.dart';

import '../../helper/Helper.dart';

class TheCheckBoxPage extends StatefulWidget {
  @override
  TheCheckBoxPage(this.title, this.values, this.c1, this.onChanged,
      {this.col = Colors.black87});
  final List<String> values;
  final String c1;
  final onStrChanged onChanged;
  final String title;
  final Color col;

  @override
  TheCheckBoxPageState createState() => new TheCheckBoxPageState();
}

class TheCheckBoxPageState extends State<TheCheckBoxPage> {
  late String checks;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.checks = widget.c1;
  }

  void changeX(bool pb1, String item) {
    if (pb1) {
      setState(() {
        checks = checks + item + "、";
      });
    } else {
      setState(() {
        checks = checks.replaceAll(item + "、", "");
      });
    }
    widget.onChanged(checks);
  }

  Widget getChecked(String title, List<String> values) {
    if (values.length == 0) return Container();
    if (title == null) title = "";

    List<Widget> items = <Widget>[];
    for (var a in values) {
      var item1 = Container(
          child: Container(
              width: 150,
              child: Row(children: [
                Checkbox(
                    value: checks.contains(a),
                    activeColor: widget.col,
                    onChanged: (pb1) {
                      changeX(pb1!, a);
                    }),
                Text(a),
              ])));
      items.add(item1);
    }

    return ListTile(
        title: Text(
          title,
          //  style: TextStyle(fontWeight: FontWeight.bold),
        ),
        subtitle: Wrap(
          spacing: 2,
          children: items,
          direction: Axis.horizontal,
        ));
  }

  @override
  Widget build(BuildContext context) {
    return getChecked(widget.title, widget.values);
  }
}
